﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModSalesman
{
    public partial class LeaveAdd : System.Web.UI.Page
    {
        SalesmanBiz salesmanBiz = new SalesmanBiz();
        //EmployeeBiz employeeBiz = new EmployeeBiz();

        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string saleManID = "0"; //通过人员列表界面，弹出界面来获得基本信息
            string leaveID = "0";
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (!IsPostBack)
            {
                if (Request.Params["SaleManID"] != null)
                {
                    saleManID = Request.Params["SaleManID"].ToString();
                }
                if (Request.Params["LeaveID"] != null)
                {
                    leaveID = Request.Params["LeaveID"].ToString();
                    ViewState["VsLeaveID"] = leaveID;

                    btnAdd.Enabled = false;
                }
                else
                {
                    btnUpdate.Enabled = false;
                    btnDelete.Enabled = false;
                }

                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的信息吗？");
                txtLeaveTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (leaveID != "0") BindData(leaveID);
                else if (saleManID != "0")
                {
                    DataTable dt = salesmanBiz.GetOneInfo(saleManID); ;
                    FillData(dt);
                }
            }
        }
        private void BindData(string leaveID)
        {
            DataTable dt = salesmanBiz.GetLeaveOneInfo(leaveID);
            if (dt.Rows.Count > 0)
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(JoinTime))
                    txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");

                txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                txtFromCompanyName.Text = dt.Rows[0]["FromCompanyName"].ToString();

                string BackAmountTime = dt.Rows[0]["BackAmountTime"].ToString();
                if (Utils.RegexRule.IsDate(BackAmountTime))
                    txtBackAmountTime.Text = Convert.ToDateTime(BackAmountTime).ToString("yyyy-MM-dd");

                string LeaveTime = dt.Rows[0]["LeaveTime"].ToString();
                if (Utils.RegexRule.IsDate(LeaveTime))
                    txtLeaveTime.Text = Convert.ToDateTime(LeaveTime).ToString("yyyy-MM-dd");

                txtDayNum.Text = dt.Rows[0]["DayNum"].ToString();
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                new DefDicBiz().InitDropDownList(ddlLeaveType, dt.Rows[0]["LeaveType"].ToString());
                new DefDicBiz().InitDropDownList(ddlIsProcedure, dt.Rows[0]["IsProcedure"].ToString());
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!salesmanBiz.IsExistEmpCode(empCode, "在职"))
            {
                JSUtility.Alert(this, "不存在该售货员编号");
                return;
            }
            string leaveTime = txtLeaveTime.Text.Trim();
            if (!RegexRule.IsDate(leaveTime))
            {
                JSUtility.Alert(this, "离职时间请输入正确的日期格式！");
                return;
            }
            string joinTime = txtJoinTime.Text.Trim();
            if (!RegexRule.IsDate(joinTime))
            {
                JSUtility.Alert(this, "入职时间请输入正确的日期格式！");
                return;
            }
            string dayNum = txtDayNum.Text.Trim();
            string hidID = hidSaleManID.Value;
            if (string.IsNullOrEmpty(hidID))
            {
                DataTable idtb = salesmanBiz.GetOneInfo(empCode, "在职");
                if (idtb.Rows.Count > 0)
                {
                    hidID = idtb.Rows[0]["SaleManID"].ToString();
                }
                else
                {
                    JSUtility.Alert(this, "输入的工号不存在！");
                    return;
                }
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SaleManID", hidID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());
            sdList.Add("JoinTime", joinTime);
            sdList.Add("LeaveTime", leaveTime);
            sdList.Add("LeaveType", ddlLeaveType.SelectedItem.Text.Trim());
            sdList.Add("DayNum", dayNum);
            sdList.Add("IsProcedure", ddlIsProcedure.SelectedItem.Text.Trim());
            sdList.Add("BackAmountTime", txtBackAmountTime.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);

            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("SaleManID", hidID);
            hList.Add("StateName", "离职");//字典中的ID 
            hList.Add("OutTime", leaveTime);
            hList.Add("DayNum", dayNum);
            hList.Add("BackAmountTime", txtBackAmountTime.Text.Trim());
            if (salesmanBiz.Update("Admin_Salesman", hList))
            {
                if (salesmanBiz.Insert("Admin_Salesman_Leave", sdList))
                {
                    salesmanBiz.UpdateInStatu(hidID, "N");
                    //更新进场信息 Statu='N'
                    JSUtility.AlertAndRedirect("增加促销员离职信息成功", "LeaveAdd.aspx");
                }
                else
                    JSUtility.Alert(this, "增加促销员离职信息[增加失败]");
            }
            else
                JSUtility.Alert(this, "增加促销员离职信息[更新基本信息失败]");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string leaveID = ViewState["VsLeaveID"].ToString();
            if (leaveID == "0")
            {
                JSUtility.Alert(this, "调用错误");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();

            string leaveTime = txtLeaveTime.Text.Trim();
            if (!RegexRule.IsDate(leaveTime))
            {
                JSUtility.Alert(this, "离职时间请输入正确的日期格式！");
                return;
            }
            string joinTime = txtJoinTime.Text.Trim();
            if (!RegexRule.IsDate(joinTime))
            {
                JSUtility.Alert(this, "入职时间请输入正确的日期格式！");
                return;
            }
            string dayNum = txtDayNum.Text.Trim();

            if (string.IsNullOrEmpty(hidSaleManID.Value))
            {
                JSUtility.Alert(this, "请从列表重新进入该界面，进行更新操作！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("LeaveID", leaveID);
            sdList.Add("SaleManID", hidSaleManID.Value);
            sdList.Add("EmpCode", txtEmpCode.Text.Trim());
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("LeaveTime", txtLeaveTime.Text.Trim());
            sdList.Add("LeaveType", ddlLeaveType.SelectedItem.Text.Trim());
            sdList.Add("DayNum", dayNum);
            sdList.Add("IsProcedure", ddlIsProcedure.SelectedItem.Text.Trim());
            sdList.Add("BackAmountTime", txtBackAmountTime.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (leaveID != "0")
            {
                if (salesmanBiz.Update("Admin_Salesman_Leave", sdList))
                {
                    SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                    hList.Add("SaleManID", hidSaleManID.Value);
                    hList.Add("StateName", "离职");//字典中的ID 
                    hList.Add("OutTime", leaveTime);
                    hList.Add("DayNum", dayNum);
                    hList.Add("BackAmountTime", txtBackAmountTime.Text.Trim());
                    salesmanBiz.Update("Admin_Salesman", hList);
                    BindData(leaveID);
                    JSUtility.Alert(this, "成功更新离职信息");
                }
                else
                    JSUtility.Alert(this, "更新离职信息失败");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string leaveID = ViewState["VsLeaveID"].ToString();
            if (leaveID == "0")
            {
                JSUtility.Alert(this, "调用错误");
                return;
            }
            string info = "";
            if (leaveID != "0")
            {
                if (salesmanBiz.DeleteLeaveOneInfo(leaveID))
                    info = "删除成功";
                else
                    info = "删除失败";
            }
            else
            { info = "请先选择记录"; }

            JSUtility.Alert(this, info);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = salesmanBiz.GetOneInfoByName(empCode, "在职");
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        FillData(dt);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = salesmanBiz.GetOneInfo(empCode, "在职");
                    if (dt.Rows.Count > 0)
                    {
                        FillData(dt);
                    }
                }
            }



        }
        private void FillData(DataTable dt)
        {
            if (dt.Rows[0]["StateName"].ToString() != "离职")
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(JoinTime))
                    txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");

                txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                txtFromCompanyName.Text = dt.Rows[0]["FromCompanyName"].ToString();
            }
            else
                JSUtility.Alert(this, "该员工已经离职，不能再次离职");
        }


    }
}